const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require('../../libs/regenerator');
const co = require("../../libs/co");
const http = require('../../libs/httpRequest');
const appConfig = require('../../appConfig');

let OrderService = function () {
  let _addOrder = function *(orderInfo) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/order/add`,
        data: orderInfo,
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }

  let _prePay = function *(orderNum,body,price) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/order/prePay`,
        data: {"orderNum":orderNum,"productName":body,"price":price},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }

  let _orderList = function *(pageIndex,pageSize,type) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/order/list`,
        data: {"pageIndex":pageIndex,"pageSize":pageSize,"type":type},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }

  let _orderInfo = function *(orderNum) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/order/info`,
        data: {"orderNum":orderNum},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    return ret;
  }

  let _doOrder = function *(orderNum,type,msg) {
    let ret = yield http.request({
        url: `${appConfig.api.baseUrl}/order/update`,
        data: {"orderNum":orderNum,"operStatus":type,"msg":msg},
        method: 'POST',
        success: (res) => {
            console.log(res.data);
        },
        fail: (err) => {
            console.log(err);
        }
    });
    console.log(ret);
    return ret.apiStatus.statusCode == 0 ? true : false;
  }

  return {
    addOrder: _addOrder,
    list: _orderList,
    info: _orderInfo,
    doOrder: _doOrder,
    prePay: _prePay
  }
}();

module.exports = OrderService;
